<template>
  <el-container class="layout-container">
    <el-aside width="200px">
      <el-menu
        :router="true"
        :default-active="$route.path"
        class="el-menu-vertical"
        text-color="#ffffff"
        active-text-color="#ffffff"
        background-color="#1e2d3d"
      >
        <div class="logo-container">
          <img src="../assets/logo.png" alt="Logo" class="logo">
        </div>
        
        <el-menu-item index="/teacher/student-results">
          <el-icon><DataLine /></el-icon>
          <span>学生测试结果</span>
        </el-menu-item>
        
        <el-menu-item index="/teacher/user-management">
          <el-icon><UserFilled /></el-icon>
          <span>用户管理</span>
        </el-menu-item>
        
        <el-menu-item 
          v-if="isAdmin" 
          index="/teacher/teacher-approval"
        >
          <el-icon><Check /></el-icon>
          <span>教师审批</span>
        </el-menu-item>
        
        <el-menu-item 
          v-if="isSchoolAdmin" 
          index="/teacher/question-management"
        >
          <el-icon><Document /></el-icon>
          <span>题库管理</span>
        </el-menu-item>
        
        <el-menu-item index="/teacher/profile">
          <el-icon><User /></el-icon>
          <span>个人信息</span>
        </el-menu-item>
      </el-menu>
    </el-aside>

    <el-container>
      <el-header>
        <div class="header-content">
          <h2>MBTI职业性格测试系统 - {{ getHeaderTitle }}</h2>
          <div class="user-info">
            <span>{{ userStore.userInfo?.fullName }}</span>
            <el-tag size="small" effect="plain" type="info" class="admin-tag">
              {{ getAdminRoleText }}
            </el-tag>
            <el-button link @click="handleLogout">退出登录</el-button>
          </div>
        </div>
      </el-header>

      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../stores/user'
import { ElMessage } from 'element-plus'
import { User, UserFilled, Document, DataLine, Check } from '@element-plus/icons-vue'

const router = useRouter()
const userStore = useUserStore()

// 获取用户的管理员角色
const adminRole = computed(() => {
  if (!userStore.userInfo?.extraInfo) return '';
  
  try {
    const extraInfo = JSON.parse(userStore.userInfo.extraInfo);
    return extraInfo.admin_role || '';
  } catch (e) {
    console.error('解析管理员角色失败:', e);
    return '';
  }
});

// 判断是否为校级管理员
const isSchoolAdmin = computed(() => {
  return userStore.userInfo?.userType?.toLowerCase() === 'super_admin' || 
         adminRole.value === 'SCHOOL_ADMIN';
});

// 判断是否为院级管理员
const isDepartmentAdmin = computed(() => {
  return adminRole.value === 'DEPARTMENT_ADMIN';
});

// 判断是否为系级管理员
const isMajorAdmin = computed(() => {
  return adminRole.value === 'MAJOR_ADMIN';
});

// 判断是否为班级管理员
const isClassAdmin = computed(() => {
  return adminRole.value === 'CLASS_ADMIN';
});

// 判断是否为管理员（系级以上）
const isAdmin = computed(() => {
  return userStore.userInfo?.userType?.toLowerCase() === 'super_admin' || 
         ['SCHOOL_ADMIN', 'DEPARTMENT_ADMIN', 'MAJOR_ADMIN'].includes(adminRole.value);
});

// 获取头部标题
const getHeaderTitle = computed(() => {
  if (userStore.userInfo?.userType?.toLowerCase() === 'super_admin') {
    return '超级管理员端';
  }
  
  if (isSchoolAdmin.value) {
    return '校级管理端';
  } else if (isDepartmentAdmin.value) {
    return '院级管理端';
  } else if (isMajorAdmin.value) {
    return '系级管理端';
  } else if (isClassAdmin.value) {
    return '班级管理端';
  } else {
    return '教师端';
  }
});

// 获取管理员角色文本
const getAdminRoleText = computed(() => {
  if (userStore.userInfo?.userType?.toLowerCase() === 'super_admin') {
    return '超级管理员';
  }
  
  if (isSchoolAdmin.value) {
    return '校级管理员';
  } else if (isDepartmentAdmin.value) {
    return '院级管理员';
  } else if (isMajorAdmin.value) {
    return '系级管理员';
  } else if (isClassAdmin.value) {
    return '班级管理员';
  } else {
    return '普通教师';
  }
});

const handleLogout = () => {
  userStore.logout()
  ElMessage.success('退出登录成功')
  router.push('/login')
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
}

.el-aside {
  background-color: #1e2d3d;
  color: #ffffff;
}

.el-menu-vertical {
  border-right: none;
  background-color: #1e2d3d;
}

.el-menu-vertical :deep(.el-menu-item) {
  color: #ffffff;
}

.el-menu-vertical :deep(.el-menu-item.is-active) {
  background-color: #1890ff;
  color: #ffffff;
}

.el-menu-vertical :deep(.el-menu-item:hover) {
  background-color: #263445;
  color: #ffffff;
}

.logo-container {
  height: 60px;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 10px 0;
}

.logo {
  height: 40px;
  width: 40px;
}

.el-header {
  background-color: #fff;
  border-bottom: 1px solid #dcdfe6;
  padding: 0 20px;
}

.header-content {
  height: 60px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.admin-tag {
  margin-left: 5px;
}

.el-main {
  padding: 20px;
  background-color: #f5f7fa;
}
</style> 